/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metro;

import cards.Card;
import cards.LimitedCard;
import enums.CardTypes;
import cards.StorageCard;
import cards.TerminalCard;
import collections.MyLinkedList;
import collections.MyList;
import enums.LimitedCardTypes;
import enums.TerminalCardTypes;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

/**
 *
 * @author Oksana_Moroz
 */
public class MetroSystem {

    private long idCart;
    private long idTourniquet;
    
    private MyList<Tourniquet> tourniquets = new MyLinkedList();
    private MyList<Card> cards = new MyLinkedList();
    private MyList<Record> permissions = new MyLinkedList();
    private MyList<Record> prohibitions = new MyLinkedList();

    protected long getNewCardID() {
        return ++idCart;
    }

    protected long getNewTourniquetID() {
        return ++idTourniquet;
    }

    public Tourniquet getTourniquet() {
        Tourniquet tourniquet = new Tourniquet(getNewTourniquetID(), this);
        tourniquets.add(tourniquet);
        return tourniquet;
    }

    public LimitedCard getFiveTripsCard(CardTypes type) {
        LimitedCard card = new LimitedCard(getNewCardID(), type, LimitedCardTypes.FIVE_TRIPS);
        cards.add(card);
        return card;
    }

    public TerminalCard getMonthCard(CardTypes type) {
        TerminalCard card = new TerminalCard(getNewCardID(), type, TerminalCardTypes.MONTH);
        cards.add(card);
        return card;
    }

    public StorageCard getStorageCard() {
        StorageCard card = new StorageCard(getNewCardID());
        cards.add(card);
        return card;
    }

    public TerminalCard getTenDaysCard(CardTypes type) {
        TerminalCard card = new TerminalCard(getNewCardID(), type, TerminalCardTypes.TEN_DAYS);
        cards.add(card);
        return card;
    }

    public LimitedCard getTenTripsCard(CardTypes type) {
        LimitedCard card = new LimitedCard(getNewCardID(), type, LimitedCardTypes.TEN_TRIPS);
        cards.add(card);
        return card;
    }

    public void addPermission(Card card, Date date, Tourniquet tourniquet) {
        permissions.add(new Record(card, date, tourniquet));
    }

    public void addProhibition(Card card, Date date, Tourniquet tourniquet) {
        prohibitions.add(new Record(card, date, tourniquet));
    }

    public void printAllData() {
        System.out.println("All cards:");
        Iterator<Card> iter = cards.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
        System.out.println();
    }

    public void printAllCardsByTypes() {
        System.out.println("All cards by types:");
        for (CardTypes type : CardTypes.values()) {
            System.out.println(type.name());
            for (Iterator<Card> it = cards.iterator(); it.hasNext();) {
                Card card = it.next();
                if (card.getType().equals(type)) {
                    System.out.println(card);
                }
            }
        }
        System.out.println();
    }

    public void printRecordsByCardTypes() {
        System.out.println("Permissions by types:");
        for (CardTypes type : CardTypes.values()) {
            System.out.println(type.name());
            for (Iterator<Card> it = cards.iterator(); it.hasNext();) {
                Card card = it.next();
                if (card.getType().equals(type)) {
                    System.out.println(card);
                }
            }
        }
        System.out.println();
        System.out.println("Prohibitions by types:");
        for (CardTypes type : CardTypes.values()) {
            System.out.println(type.name());
            for (Iterator<Card> it = cards.iterator(); it.hasNext();) {
                Card card = it.next();
                if (card.getType().equals(type)) {
                    System.out.println(card);
                }
            }
        }
        System.out.println();
    }

    protected static class Record {

        protected Card card;
        protected Date date;
        protected Tourniquet tourniquet;

        public Record(Card card, Date date, Tourniquet tourniquet) {
            this.card = card;
            this.date = date;
            this.tourniquet = tourniquet;
        }

        @Override
        public String toString() {
            return "Record: cardId = " + card.getID() + " date = " + date + " tourniquetId = "
                    + tourniquet.getID();
        }
    }
}
